package cs3343.project.family.UI;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import cs3343.project.family.FileChooser;
import cs3343.project.family.ProfileManager;

public class ProfileCreateFrame extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JTextField txt_name;
	private JTextField txt_role;
	private JRadioButton rdbtnMale;
	private JRadioButton rdbtnFemale;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ProfileCreateFrame frame = new ProfileCreateFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ProfileCreateFrame() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 400, 400);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		//contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_contentPane.columnWeights = new double[] { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		contentPane.setLayout(gbl_contentPane);
		
		JLabel lblCreateNewProfile = new JLabel("Create New Profile");
		GridBagConstraints gbc_lblCreateNewProfile = new GridBagConstraints();
		gbc_lblCreateNewProfile.gridwidth = 7;
		gbc_lblCreateNewProfile.insets = new Insets(0, 0, 5, 0);
		gbc_lblCreateNewProfile.gridx = 0;
		gbc_lblCreateNewProfile.gridy = 0;
		contentPane.add(lblCreateNewProfile, gbc_lblCreateNewProfile);
		
		JLabel lbl_name = new JLabel("Name:");
		GridBagConstraints gbc_lbl_name = new GridBagConstraints();
		gbc_lbl_name.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_name.gridx = 0;
		gbc_lbl_name.gridy = 2;
		contentPane.add(lbl_name, gbc_lbl_name);
		
		txt_name = new JTextField();
		GridBagConstraints gbc_txt_name = new GridBagConstraints();
		gbc_txt_name.gridwidth = 4;
		gbc_txt_name.insets = new Insets(0, 0, 5, 5);
		gbc_txt_name.fill = GridBagConstraints.HORIZONTAL;
		gbc_txt_name.gridx = 2;
		gbc_txt_name.gridy = 2;
		contentPane.add(txt_name, gbc_txt_name);
		txt_name.setColumns(10);
		
		JLabel lbl_gender = new JLabel("Gender:");
		GridBagConstraints gbc_lbl_gender = new GridBagConstraints();
		gbc_lbl_gender.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_gender.gridx = 0;
		gbc_lbl_gender.gridy = 4;
		contentPane.add(lbl_gender, gbc_lbl_gender);
		
		rdbtnMale = new JRadioButton("Male");
		GridBagConstraints gbc_rdbtnMale = new GridBagConstraints();
		gbc_rdbtnMale.gridwidth = 2;
		gbc_rdbtnMale.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnMale.gridx = 2;
		gbc_rdbtnMale.gridy = 4;
		contentPane.add(rdbtnMale, gbc_rdbtnMale);
		
		rdbtnFemale = new JRadioButton("Female");
		GridBagConstraints gbc_rdbtnFemale = new GridBagConstraints();
		gbc_rdbtnFemale.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnFemale.gridx = 4;
		gbc_rdbtnFemale.gridy = 4;
		contentPane.add(rdbtnFemale, gbc_rdbtnFemale);
		
		JLabel lbl_role = new JLabel("Family Role:");
		GridBagConstraints gbc_lbl_role = new GridBagConstraints();
		gbc_lbl_role.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_role.gridx = 0;
		gbc_lbl_role.gridy = 6;
		contentPane.add(lbl_role, gbc_lbl_role);
		
		txt_role = new JTextField();
		GridBagConstraints gbc_txt_role = new GridBagConstraints();
		gbc_txt_role.gridwidth = 4;
		gbc_txt_role.insets = new Insets(0, 0, 5, 5);
		gbc_txt_role.fill = GridBagConstraints.HORIZONTAL;
		gbc_txt_role.gridx = 2;
		gbc_txt_role.gridy = 6;
		contentPane.add(txt_role, gbc_txt_role);
		txt_role.setColumns(10);
		
		JLabel lbl_icon = new JLabel("Image:");
		GridBagConstraints gbc_lbl_icon = new GridBagConstraints();
		gbc_lbl_icon.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_icon.gridx = 0;
		gbc_lbl_icon.gridy = 8;
		contentPane.add(lbl_icon, gbc_lbl_icon);
		
		JLabel lblNoImageSelected = new JLabel("No image selected");
		GridBagConstraints gbc_lblNoImageSelected = new GridBagConstraints();
		gbc_lblNoImageSelected.insets = new Insets(0, 0, 5, 5);
		gbc_lblNoImageSelected.gridx = 2;
		gbc_lblNoImageSelected.gridy = 8;
		contentPane.add(lblNoImageSelected, gbc_lblNoImageSelected);
		
		JButton btnNewButton = new JButton("Selection Profile Image");
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.gridwidth = 3;
		gbc_btnNewButton.insets = new Insets(0, 0, 5, 5);
		gbc_btnNewButton.gridx = 3;
		gbc_btnNewButton.gridy = 8;
		contentPane.add(btnNewButton, gbc_btnNewButton);
		
		JButton btn_create = new JButton("Create");
		btn_create.addActionListener(this);
		
		GridBagConstraints gbc_btn_create = new GridBagConstraints();
		gbc_btn_create.insets = new Insets(0, 0, 0, 5);
		gbc_btn_create.gridx = 4;
		gbc_btn_create.gridy = 10;
		contentPane.add(btn_create, gbc_btn_create);
		
		btnNewButton.addActionListener(new ActionListener() {
			  public void actionPerformed(ActionEvent e) {
				  new FileChooser();
			  }
		});		
	}
	
	public void actionPerformed(ActionEvent e) {
		if (!submitForm())
			; // TODO: Error Message
		
		else
			; // TODO: Successful Message
	}
	
	private boolean submitForm() {
		String gender = "";
		if (rdbtnMale.isSelected())
			gender = "M";
		else if (rdbtnFemale.isSelected())
			gender = "F";
		
		if (!txt_name.getText().isEmpty() && gender != "" && !txt_role.getText().isEmpty())
			ProfileManager.createNewProfile(txt_name.getText(), gender ,txt_role.getText());
		// TODO: if name existed?
		else
			return false;
		return true;
	}

}
